package com.pengheng.controller;

import cn.hutool.core.date.DateUtil;
import com.pengheng.bean.Document;
import com.pengheng.easyes.mapper.DocumentMapper;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class EasyESController {

    @Resource
    private DocumentMapper documentMapper;


    private LambdaEsQueryWrapper<Document> getQueryWrapper() {
        String index = getIndex();
        LambdaEsQueryWrapper<Document> queryWrapper = new LambdaEsQueryWrapper<>();
        queryWrapper.index(index);
        return queryWrapper;
    }

    public String getIndex() {
        return "document-" + DateUtil.today();
    }

    @GetMapping("/createIndex")
    public String createIndex() {
        String index = getIndex();
        Boolean exists = documentMapper.existsIndex(index);
        if (exists) {
            return "已存在索引";
        } else {
            documentMapper.createIndex(index);

            return "创建的索引为：" + index;
        }
    }

    /**
     * {
     * "id":"1001",
     * "title":"老汉",
     * "age":54,
     * "size":18,
     * "content":"推车技术过硬"
     * }
     * 保存数据
     *
     * @param document
     * @return
     */
    @PostMapping("/insert")
    public Integer insert(@RequestBody Document document) {
        String index = getIndex();
        return documentMapper.insert(document, index);
    }

    @GetMapping("/search")
    public List<Document> searchAll() {
        LambdaEsQueryWrapper<Document> wrapper = getQueryWrapper();
        return documentMapper.selectList(wrapper);
    }

    /**
     * 嵌套 and 查询
     *
     * @return
     */
    @GetMapping("/nestedAnd")
    public List<Document> nestedAnd() {
        LambdaEsQueryWrapper<Document> queryWrapper = getQueryWrapper();
        queryWrapper.in(Document::getType, 1, 2)
                .and(w -> w.eq(Document::getTitle, "老汉").or().eq(Document::getTitle, "老王"));
        return documentMapper.selectList(queryWrapper);
    }

    /**
     * 拼接and
     */
    @GetMapping("/connectAnd")
    public List<Document> connectAnd() {
        LambdaEsQueryWrapper<Document> queryWrapper = getQueryWrapper();
        queryWrapper.eq(Document::getTitle, "老汉")
//                .match(Document::getType,1);
                .eq(Document::getType, 1);
        return documentMapper.selectList(queryWrapper);

    }

    /**
     * 嵌套 or
     */
    @GetMapping("/nestedOr")
    public List<Document> nestedOr() {
        LambdaEsQueryWrapper<Document> queryWrapper = getQueryWrapper();
        queryWrapper.eq(Document::getType, 1)
                .or(i -> i.eq(Document::getTitle, "老汉").eq(Document::getSize, 18));
        return documentMapper.selectList(queryWrapper);
    }

    /**
     * 拼接 or
     */
    @GetMapping("/connectOr")
    public List<Document> connectOr() {
        LambdaEsQueryWrapper<Document> queryWrapper = getQueryWrapper();
        queryWrapper.eq(Document::getTitle, "老王").or()
                .eq(Document::getTitle, "老李");
        return documentMapper.selectList(queryWrapper);
    }

    /**
     * 拼接 or
     */
    @GetMapping("/nestedFilter")
    public List<Document> nestedFilter() {
        LambdaEsQueryWrapper<Document> queryWrapper = getQueryWrapper();
        queryWrapper.in(Document::getType, 1, 2)
                .filter(w -> w.eq(Document::getTitle, "老王").or().eq(Document::getTitle, "老李"));
        return documentMapper.selectList(queryWrapper);
    }

    /**
     * 拼接 filter
     */
    @GetMapping("/filter")
    public List<Document> filter() {
        LambdaEsQueryWrapper<Document> queryWrapper = getQueryWrapper();
//        queryWrapper.eq(Document::getTitle, "老王");
        queryWrapper.filter().eq(Document::getTitle, "老王");
//        queryWrapper.filter(i->i.eq(Document::getTitle,"老王"));
        return documentMapper.selectList(queryWrapper);
    }

    /**
     * 嵌套 not
     */
    @GetMapping("/nestedNot")
    public List<Document> nestedNot() {
        LambdaEsQueryWrapper<Document> queryWrapper = getQueryWrapper();
        queryWrapper.not(i -> i.eq(Document::getAge, 18).eq(Document::getType, 1));
        return documentMapper.selectList(queryWrapper);
    }


    /**
     * not
     */
    @GetMapping("/not")
    public List<Document> not() {
        LambdaEsQueryWrapper<Document> queryWrapper = getQueryWrapper();
        queryWrapper.eq(Document::getTitle, "老王")
                .not()
                .eq(Document::getSize, 18);
        return documentMapper.selectList(queryWrapper);
    }

    /**
     * 混合查询
     */
    @GetMapping("/mixedSearch")
    public List<Document> mixedSearch() {
        LambdaEsQueryWrapper<Document> queryWrapper = getQueryWrapper();
        queryWrapper.eq(Document::getType, 2);
        SearchSourceBuilder searchSourceBuilder = documentMapper.getSearchSourceBuilder(queryWrapper);
        //如果重新查询会覆盖之前的查询条件
//        searchSourceBuilder.query(QueryBuilders.termQuery("age", 18));
        searchSourceBuilder.sort("age", SortOrder.DESC).size(0);

        queryWrapper.setSearchSourceBuilder(searchSourceBuilder);
        return documentMapper.selectList(queryWrapper);
    }

}
